package logic

import (
	"leechbox_gis/tools"
	"strings"
)

var ExecuteLogic = newExecuteLogic()

func newExecuteLogic() *executeLogic {
	return &executeLogic{}
}

type executeLogic struct {
}

func (*executeLogic) ExecuteCommandSync(command string, args ...string) (string, error) {
	fullCommand := strings.Join(args, " ")
	return tools.Execute(command + " " + fullCommand)
}

func (*executeLogic) ExecuteCommandAsync(randomCode string, command string, args ...string) (string, error) {
	fullCommand := command + " " + strings.Join(args, " ") + " >> /tmp/" + randomCode
	go tools.Execute(fullCommand)
	return "", nil
}

func (*executeLogic) GetAsyncExecuteResult(randomCode string) string {
	content, _ := tools.Execute("cat /tmp/" + randomCode)
	return content
}
